#!/usr/bin/python3
# -*- coding: utf-8 -*-
#pip3 install jinja2
import socket
import jinja2
import sys,shutil
zabbix_server = "120.26.88.141"
zabbix_path = "/usr/local/zabbix/conf/"
host_name_prefix = "app"    #设置hostname前缀，只是针对zabbix配置
def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        s.connect(('10.0.0.1',8080))
        ip= s.getsockname()[0]
    finally:
        s.close()
    return ip
local_ip = get_host_ip()
host_name = host_name_prefix + local_ip #hostname 以前缀+本机内网ip地址
env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0]))
template = env.get_template('template.conf')
html = template.render(local_ip=local_ip, zabbix_server=zabbix_server,host_name=host_name)
f2 = sys.path[0] + "/zabbix_agentd.conf"
with open(f2,'w') as fp:
    fp.write(html)
srcfile = sys.path[0] +  "/" + "zabbix_agentd.conf"
dstfile = zabbix_path + "/" + "zabbix_agentd.conf"
shutil.move(srcfile,dstfile)
